package com.joinlabs.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.joinlabs.entity.Admin;
import com.joinlabs.entity.Log;
import com.joinlabs.entity.LogDataByLayUITableBean;
import com.joinlabs.entity.PageBean;
import com.joinlabs.entity.User;
import com.joinlabs.entity.UserDataByLayUITableBean;
import com.joinlabs.entity.Weibo;
import com.joinlabs.entity.WeiboDataByLayUITableBean;
import com.joinlabs.entity.WeiboUser;
import com.joinlabs.exception.CustomException;
import com.joinlabs.service.IAdminService;

@Controller
@RequestMapping("/login")
@SessionAttributes(names= {"admin","user","log","weibo"}) //声明要操作session域中的对象名
public class AdminLogicAction {

	@Resource
	private IAdminService adminService;
	
	@RequestMapping("/checkLogin")
	public ModelAndView checkLogin(Admin admin,ModelMap map) {
		System.out.println("登陆前："+admin);
		
		try {
			Admin successAdmin = adminService.checkLogin(admin);
			
			map.addAttribute("admin", successAdmin);
			
			return new ModelAndView("redirect:/admin/index");
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			map.addAttribute("error",e.getMessage());
			
			return new ModelAndView("/admin/login");
		}
				
	}
	
	//后台显示用户数据
	@RequestMapping("/show_user_table")
	@ResponseBody
	public Object showUserTable(PageBean pageBean) {
		System.out.println(pageBean);
		UserDataByLayUITableBean bean = adminService.findUsersByTableDate(pageBean);
		return bean;
	}
	
	//后台显示拉黑用户数据
	@RequestMapping("/show_blackuser_table")
	@ResponseBody
	public Object showBlackUserTable(PageBean pageBean) {
		System.out.println(pageBean);
		UserDataByLayUITableBean bean = adminService.findBlackUsersByTableDate(pageBean);
		return bean;
	}
	
	//后台显示微博数据
	@RequestMapping("/show_weibo_table")
	@ResponseBody
	public Object showWeiboTable(PageBean pageBean) {
		System.out.println(pageBean);
		WeiboDataByLayUITableBean bean = adminService.findWeibosByTableDate(pageBean);
		return bean;
		
	}
	
	//后台显示文章数据
	@RequestMapping("/show_log_table")
	@ResponseBody
	public Object showlogTable(PageBean pageBean) {		
		System.out.println(pageBean);
		LogDataByLayUITableBean bean = adminService.findLogsByTableDate(pageBean);
		return bean;
	}
	
	//查看用户详情界面
	@RequestMapping("/show_viewuser")
	public ModelAndView showViewUser(long userId,ModelMap map) {
		User user = adminService.showUserByPrimaryKey(userId);	
		System.out.println("-----------------------"+user);
		map.addAttribute("user",user);
		return new ModelAndView("/admin/show_viewuser");
	}
	
	//查看文章详情界面
	@RequestMapping("/show_viewlog")
	public ModelAndView showViewLog(long logId,ModelMap map) {
		Log log = adminService.showLogByPrimaryKey(logId);	
		System.out.println("-----------------------"+log);
		map.addAttribute("log",log);
		return new ModelAndView("/admin/show_viewlog");
	}
	
	//查看动态详情界面
	@RequestMapping("/show_viewweibo")
	public ModelAndView showViewWeibo(long weiboId,ModelMap map) {
		WeiboUser weibo = adminService.showWeiboByPrimaryKey(weiboId);	
		System.out.println("-----------------------"+weibo);
		map.addAttribute("weibo",weibo);
		return new ModelAndView("/admin/show_viewweibo");
	}
	
	//拉黑一个用户
	@RequestMapping("/deluser")
	@ResponseBody
	public Object delUser(long userId) {
		System.out.println("++++++++++++"+userId);
		int row = adminService.updateUserByUserId(userId);
		System.out.println(row);
		return "success";	
	}
	
	//拉黑多个用户
	@RequestMapping("/delusers")
	@ResponseBody
	public Object delUsers(String[] ids) {
		for(int i = 0;i<ids.length-1;i++) {
			long id = Long.parseLong(ids[i]);
			int row = adminService.updateUserByUserId(id);
		}
		return "success";	
	}
	
	//取消拉黑一个用户
	@RequestMapping("/updateuser")
	@ResponseBody
	public Object updateUser(long userId) {
		System.out.println("++++++++++++"+userId);
		int row = adminService.updataBlackUserByUserId(userId);
		System.out.println(row);
		return "success";	
	}
	
	//取消拉黑多个用户
	@RequestMapping("/updateusers")
	@ResponseBody
	public Object updateUsers(String[] ids) {
		for(int i = 0;i<ids.length-1;i++) {
			long id = Long.parseLong(ids[i]);
			int row = adminService.updataBlackUserByUserId(id);
		}
		return "success";	
	}
	
	
	//删除一个文章
	@RequestMapping("/dellog")
	@ResponseBody
	public Object delLog(long logId) {
		System.out.println(logId);
		int row = adminService.delLogByPrimaryKey(logId);
		System.out.println(row);
		return "success";	
	}
	
	//删除多个文章
	@RequestMapping("/dellogs")
	@ResponseBody
	public Object delLogs(String[] ids) {
		
		for(int i = 0;i<ids.length-1;i++) {
			long id = Long.parseLong(ids[i]);
			int row = adminService.delLogByPrimaryKey(id);
		}
		return "success";	
	}
	
	//删除一个动态
	@RequestMapping("/delweibo")
	@ResponseBody
	public Object delweibo(long weiboId) {
		System.out.println(weiboId);
		int row = adminService.delWeiboByPrimaryKey(weiboId);
		System.out.println(row);
		return "success";	
	}
	
	//删除多个动态
	@RequestMapping("/delweibos")
	@ResponseBody
	public Object delWeibos(String[] ids) {
			
		for(int i = 0;i<ids.length-1;i++) {
			long id = Long.parseLong(ids[i]);
			int row = adminService.delWeiboByPrimaryKey(id);
		}
		return "success";	
	}
}
